﻿using System;
using System.Collections.Generic;
using System.Linq;
using Notoric.Commanding;
using Notoric.Gui.Windows;
using Notoric.ViewModel.Elements.CWNotation;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command that beams a selected group of durational symbols.
    /// </summary>
    public class MakeBeamGroupCommand : MusicSelectionRibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MakeBeamGroupCommand"/> class.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// The main application window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public MakeBeamGroupCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
            ScreenTipTitle = "Beam Group";
            ScreenTipText = "Connects selected group of notes by a beam.";
            DisableReason = "Select two or more notes first.";
        }

        #endregion

        #region MusicSelectionRibbonCommand Members

        /// <inheritdoc/>
        protected override bool CanExecuteCore(object parameter)
        {
            var durSymbols = from symbol in Selection
                             where symbol is DurationalSymbol
                             select symbol as DurationalSymbol;

            return durSymbols.Count(s => s.BasicDuration <= 16) >= 2;
        }

        #endregion

        #region ICommand Members
        
        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            var symbols = Selection.GroupByPart();
            var macro = new UndoableMacroCommand(ApplicationWindow.StackManager.ActiveUndoStack, ApplicationWindow.StackManager.ActiveRedoStack);

            foreach (var gr in symbols)
            {
                var durSyms = from symbol in gr.Symbols.OrderByBeat()
                              where symbol is DurationalSymbol && ((DurationalSymbol)symbol).BasicDuration <= 16
                              group symbol by ((DurationalSymbol)symbol).Voice into g
                              select g;

                foreach (var fs in durSyms)
                {
                    var beginning = fs.FirstOrDefault(s => s is Chord);
                    var end = fs.LastOrDefault(s => s is Chord);
                    if (beginning == null || beginning == end)
                        continue;

                    var symbolsList = new List<DurationalSymbol>();
                    bool bTake = false;
                    foreach (var symbol in fs)
                    {
                        if (symbol == beginning)
                            bTake = true;

                        if (bTake)
                            symbolsList.Add(symbol as DurationalSymbol);

                        if (symbol == end)
                            break;
                    }

                    macro.Commands.Add(
                        new ViewModel.MakeBeamGroupCommand(ApplicationWindow.StackManager.ActiveUndoStack, ApplicationWindow.StackManager.ActiveRedoStack)
                        {
                            Symbols = symbolsList
                        }
                        );
                }
            }

            try
            {
                macro.Execute(null);
            }
            catch (Exception)
            {
            }
        }

        #endregion
    }
}
